package mitchellcam1;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;

public class Zone 
{
	String zoneId;
	double radiusMeters;
	double radius;
	double latitude;
	double longitude;
	int occupants;
	int choice;
	int counter;
	
	//default constructor
	public Zone()
	{
		setzoneId("");
		setRadius(0);
		setLatitude(0);
		setLongitude(0);
		setOccupants(0);
	}
	
	public Zone(String zoneId, double radius, double latitude, double longitude, int occupants) 
	{
		this.zoneId = zoneId;
		this.radius = radius;
		this.latitude = latitude;
		this.longitude = longitude;
		this.occupants = occupants;
	}

	//gets and sets
	public String getzoneId() {
		return zoneId;
	}
	public void setzoneId(String zoneId) {
		this.zoneId = zoneId;
	}
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
	public double getLatitude() {
		return latitude;
	}
	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}
	public double getLongitude() {
		return longitude;
	}
	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}
	public int getOccupants() {
		return occupants;
	}
	public void setOccupants(int occupants) {
		this.occupants = occupants;
	}
	
	public void setup(String incomingZoneId)
	{
		zoneId = incomingZoneId;
		if (zoneId == "null")
		{
			System.out.println();
			System.out.println("Input zone ID:");
			Scanner in = new Scanner(System.in);
			zoneId = in.nextLine();
			
			//find out if this is a saved user
			System.out.println("Is this a previous zone you want to import from a text file?");
			System.out.println("1.Yes");
			System.out.println("2.No");
			in = new Scanner(System.in);
			choice = in.nextInt();
		}
		else
		{
			zoneId = incomingZoneId;
			choice = 1;
		}
		
		if (choice == 2)
		{
			try
			{
				System.out.println();
				System.out.println("Input zone latitude: (degrees)");
				Scanner in = new Scanner(System.in);
				latitude = in.nextDouble();
				
				System.out.println();
				System.out.println("Input zone longitude (degrees):");
				in = new Scanner(System.in);
				longitude = in.nextDouble();
				
				System.out.println();
				System.out.println("Input zone radius (meters):");
				in = new Scanner(System.in);
				radiusMeters = in.nextDouble();
				
				//write user info to a file
				new File(zoneId + ".zone");
				FileOutputStream outputstream = new FileOutputStream(zoneId +".zone");
				PrintWriter fileWriter = new PrintWriter(outputstream);
				fileWriter.print(radiusMeters+"\n");
				fileWriter.print(latitude+"\n");
				fileWriter.print(longitude+"\n");
				fileWriter.close();
			}
			catch (FileNotFoundException e) 
			{
				e.printStackTrace();
			}
		}
		if (choice == 1)
		{
			//read user info from the appropriate file
			try
			{
			    // Open the file that is the first 
			    // command line parameter
			    FileInputStream fstream = new FileInputStream(zoneId + ".zone");
			    // Get the object of DataInputStream
			    DataInputStream stream = new DataInputStream(fstream);
			    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
			    String strLine;
			    //Read File Line By Line
			    counter = 1;
			    while ((strLine = br.readLine()) != null)   
			    {
			      switch (counter)
			      {	
			      	case 1: radiusMeters = Double.parseDouble(strLine);
			      			break;
			      			
			      	case 2: latitude = Double.parseDouble(strLine);
			      			break;
			      			
			      	case 3: longitude = Double.parseDouble(strLine);
			      			break;
			      }
			      counter++;
			    }
			    //Close the input stream
			    stream.close();
			   
			}
			catch(Exception e)
			{
				System.out.println("Zone file non-existent or otherwise no-good!");
				e.printStackTrace();
			}
			radius = .0000106 * radiusMeters;
		}
	}
	
	
	public void controlzone()
	{

	}
}
